package com.xywy.fwc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.fwc.bean.FwcItem;

public class FwcWebView extends Activity {
	private TextView title;
	private FwcItem fi ;
	private WebView wv;
	private ProgressBar progressBar;
	private String currentUrl;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	setContentView(R.layout.fwc_web);
    	title = (TextView) findViewById(R.id.tv_title);
    	wv = (WebView) findViewById(R.id.webview);
    	progressBar = (ProgressBar) findViewById(R.id.progressBar);
    	findViewById(R.id.tv_title_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(currentUrl.equals(fi.getUrl())){
					finish();
				}else {
					wv.goBack();
				}
			}
		});
    	findViewById(R.id.tv_title_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				 finish();
			}
		});
    	Intent intent = getIntent();
    	fi = (FwcItem) intent.getSerializableExtra("item");
    	title.setText(fi.getTitle());
    	setJsSupport();
    	currentUrl = fi.getUrl();
    	
    	System.out.println("url---  "+fi.getUrl());
    	
    	wv.loadUrl(fi.getUrl());
//    	wv.loadUrl("http://service.weibo.com/share/mobile.php?appkey=1343713053&searchPic=true");
    }
	private void setJsSupport() {
		wv.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				progressBar.setVisibility(View.GONE);
			}
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				currentUrl = url;
				progressBar.setProgress(1);
			}
		});
		wv.setWebChromeClient(new WebChromeClient(){

			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					JsResult result) {
				// TODO Auto-generated method stub
				return super.onJsAlert(view, url, message, result);
			}

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
				progressBar.setProgress(newProgress);
			}
			@Override
			public void onReceivedTitle(WebView view, String t) {
				// TODO Auto-generated method stub
				super.onReceivedTitle(view, t);
				if(t != null)
				title.setText(t);
			}
		});
		 WebSettings s = wv.getSettings();    
		 s.setBuiltInZoomControls(true);     
		 s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);     s.setUseWideViewPort(true);     s.setLoadWithOverviewMode(true);     s.setSavePassword(true);     s.setSaveFormData(true);     s.setJavaScriptEnabled(true);     // enable navigator.geolocation     s.setGeolocationEnabled(true);     s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");     // enable Web Storage: localStorage, sessionStorage     s.setDomStorageEnabled(true);  wb.requestFocus();  wb.setScrollBarStyle(0);
	
	}
	
	@Override
	  public boolean onKeyDown(int keyCode, KeyEvent event) {
	   if (keyCode == KeyEvent.KEYCODE_BACK && wv.canGoBack()) {
		   wv.goBack();// 返回前一个页面
	   return true;
	   }
	   return super.onKeyDown(keyCode, event);
	  } 
}
